package com.ruoyi.project.api.controller;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.api.domain.RealName;
import com.ruoyi.project.user.domain.UserInfo;
import com.ruoyi.project.user.service.IUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @title : RealNameController
 * @Author : Administrator
 * @Date : 2025/4/9
 * @Version : 1.0.0
 * @Description : 实名验证
 */
@RestController
@RequestMapping("/api/realName")
public class RealNameController {

    @Autowired
    private IUserInfoService userInfoService;
    private Logger logger = LoggerFactory.getLogger(RealNameController.class);
    @RequestMapping("/add")
    public AjaxResult addRealName(@RequestBody RealName realName) {
        logger.info("addRealName:{}",realName);
        String name = realName.getName();
        String idNumber = realName.getIdNumber();
        String mobile = realName.getMobile();
        String driverNumber = realName.getDriverNumber();
        String issueDate = realName.getIssueDate();
        String expiryDate = realName.getExpiryDate();
        List<String> idCardFiles = realName.getIdCardFiles();
        List<String> driverFiles = realName.getDriverFiles();
        UserInfo userInfo = new UserInfo();

        userInfo.setDriverIdcard(idNumber);
        userInfo.setDriverName(name);
        userInfo.setDriverContact(mobile);
        userInfo.setDrivingLicenseNo(driverNumber);
        userInfo.setDrivingLicenseFirstDate(issueDate);
        userInfo.setDrivingLicenseExpiryDate(expiryDate);
        if (idCardFiles.size()<2){
            return AjaxResult.error("身份证照片数量不足");
        }
        userInfo.setPhotoCode(idCardFiles.get(0));
        userInfo.setRes2(idCardFiles.get(1));
        if (driverFiles.size()<1){
            return AjaxResult.error("驾驶证照片数量不足");
        }
        userInfo.setDrivingLicenseImg(driverFiles.get(0));
        userInfo.setDriverStatus("0");
        if (StringUtils.isBlank(userInfo.getDriverContact())){
            return AjaxResult.error("手机号不能为空");
        }else{
            userInfo.setUserId(userInfo.getDriverContact());
        }
        int count = userInfoService.insertUserInfo(userInfo);
        if (count == 0){
            return AjaxResult.error("实名信息存储失败");
        }
        return AjaxResult.success(realName);
    }

}
